from square import Square
from team import Team
from piece import PlayerPiece, BallPiece
from commonerror import BoardError

class Board:
    """ Represents the actual Mastergoal board """
    
    class Config:
    
        (GOAL, SPECIAL, LITTLE, BIG, CORNER, AREA, CENTRAL) = range(7)
        
        areas = [
            [ AREA, AREA, AREA, GOAL, GOAL, GOAL, GOAL, GOAL, AREA, AREA, AREA ],
            [ CORNER, BIG, LITTLE, SPECIAL, SPECIAL, SPECIAL, SPECIAL, SPECIAL, LITTLE, BIG, CORNER ],
            [ AREA, BIG, LITTLE, LITTLE, LITTLE, LITTLE, LITTLE, LITTLE, LITTLE, BIG, AREA ],
            [ AREA, BIG, BIG, BIG, BIG, BIG, BIG, BIG, BIG, BIG, AREA ],
            [ AREA, BIG, BIG, BIG, BIG, BIG, BIG, BIG, BIG, BIG, AREA ],
            [ AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA ],
            [ AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA ],
            [ CENTRAL, CENTRAL, CENTRAL, CENTRAL, CENTRAL, CENTRAL, CENTRAL, CENTRAL, CENTRAL, CENTRAL, CENTRAL ],
            [ AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA ],
            [ AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA, AREA ],     
            [ AREA, BIG, BIG, BIG, BIG, BIG, BIG, BIG, BIG, BIG, AREA ],
            [ AREA, BIG, BIG, BIG, BIG, BIG, BIG, BIG, BIG, BIG, AREA ],
            [ AREA, BIG, LITTLE, LITTLE, LITTLE, LITTLE, LITTLE, LITTLE, LITTLE, BIG, AREA ],
            [ CORNER, BIG, LITTLE, SPECIAL, SPECIAL, SPECIAL, SPECIAL, SPECIAL, LITTLE, BIG, CORNER ],
            [ AREA, AREA, AREA, GOAL, GOAL, GOAL, GOAL, GOAL, AREA, AREA, AREA ]
            ]
        
#        areas = [
#            [ AREA, CORNER, AREA, AREA, AREA, AREA, AREA, CENTRAL, AREA, AREA, AREA, AREA, AREA, CORNER, AREA ],
#            [ AREA, BIG, BIG ,BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, BIG, BIG, AREA ],
#            [ AREA, LITTLE, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, LITTLE, AREA ],
#            [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
#            [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
#            [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
#            [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
#            [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
#            [ AREA, LITTLE, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, LITTLE, AREA ],
#            [ AREA, BIG, BIG, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, BIG, BIG, AREA ],
#            [ AREA, CORNER, AREA, AREA, AREA, AREA, AREA, CENTRAL, AREA, AREA, AREA, AREA, AREA, CORNER, AREA ]           
#        ]
        
    
    def __init__(self, ballpiece):
        self.teams = {}
        self.player_with_ball = None
        self.ballpiece = ballpiece
        
    def get_ball(self):
        return self.ballpiece
        
    def __getitem__(self, key):
        return self.teams[key]
        
    def add_team(self, team):
        if len(self.teams) > 1:
            raise BoardError("only two teams per board are allowed")

        self.teams[team.orientation] = team 
        
    def remove_team(self, team):
        del self.teams[team.orientation]
        
    def get_keepers(self):
        keepers = []
        for team in self.teams:
            for player in team.players:
                if player.id == PlayerPiece.G:
                    keepers.append(player)
        return keepers
            
    @staticmethod
    def own_corner(orientation, row, col):
        return (orientation == Team.DOWN and Board.Config.areas[row][col] == Board.Config.CORNER and row == 1) or (orientation == Team.UP and Board.Config.areas[row][col] == Board.Config.CORNER and row == 13)
    
    @staticmethod
    def big_area(orientation, row, col):
        return (orientation == Team.DOWN and Board.Config.areas[row][col] <= Board.Config.BIG and row < 7) or (orientation == Team.UP and Board.Config.areas[row][col] <= Board.Config.BIG and row > 7)
    
    @staticmethod
    def little_area(row, col):
        return ((orientation == Team.DOWN and Board.Config.areas[row][col] <= Board.Config.LITTLE and row < 7)
            or (orientation == Team.UP and Board.Config.areas[row][col] <= Board.Config.LITTLE and row > 7))
    
    @staticmethod
    def team_area(orientation, row, col):
         return (orientation == Team.DOWN and Board.Config.areas[row][col] <= Board.Config.AREA and row < 7) or (orientation == Team.UP and Board.Config.areas[row][col] <= Board.Config.AREA and row > 7)
     

    @staticmethod
    def is_goal(orientation, row, col):
        return (
            (orientation == Team.DOWN and Board.Config.areas[row][col] == Board.Config.GOAL)
            or (orientation == Team.UP and Board.Config.areas[row][col] == Board.Config.GOAL)
            )
        
    @staticmethod
    def special_square(orientation, row, col):
        return (
                (orientation == Team.DOWN and (Board.Config.areas[row][col] == Board.Config.CORNER or 
                                             Board.Config.areas[row][col]== Board.Config.SPECIAL) and row == 13) or 
                (orientation == Team.UP and (Board.Config.areas[row][col] == Board.Config.CORNER or 
                                               Board.Config.areas[row][col] == Board.Config.SPECIAL) and row == 1)
            )
    
    @staticmethod
    def opponent_in_square(self, orientation, col, row):
        
        opponent = orientation and Team.UP or Team.DOWN
        for player in self.teams[opponent].players:
            if player.square == Square(row, col):
                return True
            
        return False
        
if __name__ == "__main__":
       
    print Board.Config.areas[0][1]     
        
        
    